//Thu May 19 11:12:55 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int dx[] = { 0, 0, 1, -1, 0, 0, 0, 0 };
int dy[] = { 0, 0, 0, 0, 1, -1, 0, 0 };
int dz[] = { 0, 0, 0, 0, 0, 0, 1, -1 };
int dw[] = { 1, -1, 0, 0, 0, 0, 0, 0 };

class State {
public:
	int a;
	int b;
	int c;
	int d;
	State(string s) {
		this->a = s[0] - 'a';
		this->b = s[1] - 'a';
		this->c = s[2] - 'a';
		this->d = s[3] - 'a';
	}
	State(int aa, int bb, int cc, int dd) {
		this->a = aa;
		this->b = bb;
		this->c = cc;
		this->d = dd;
	}
	bool equals(State s) {
		return this->a == s.a && this->b == s.b && this->c == s.c && this->d
				== s.d;
	}
};

class SmartWordToy {

private:
	bool forbiden[26][26][26][26];
	int cost[26][26][26][26];
public:
	int minPresses(string start, string finish, vector<string> forbid) {
		init(forbid);
		//		if (forbiden[start[0] - 'a'][start[1] - 'a'][start[2] - 'a'][start[3]
		//				- 'a'])
		//			return -1;
		State st(start);
		cost[start[0] - 'a'][start[1] - 'a'][start[2] - 'a'][start[3] - 'a']
				= 0;
		queue<State> q;
		q.push(st);
		while (!q.empty()) {
			int a = q.front().a;
			int b = q.front().b;
			int c = q.front().c;
			int d = q.front().d;
			int dist = cost[a][b][c][d];
			q.pop();
			for (int i = 0; i < 8; i++) {
				int x = (a + dx[i] + 26) % 26;
				int y = (b + dy[i] + 26) % 26;
				int z = (c + dz[i] + 26) % 26;
				int w = (d + dw[i] + 26) % 26;
				State tmpST(x, y, z, w);
				if (forbiden[x][y][z][w] || cost[x][y][z][w] != -1)
					continue;
				cost[x][y][z][w] = dist + 1;
				q.push(tmpST);
			}
		}
		return cost[finish[0] - 'a'][finish[1] - 'a'][finish[2] - 'a'][finish[3]
				- 'a'];
	}

	void init(vector<string> f) {
		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 26; j++) {
				for (int p = 0; p < 26; p++) {
					for (int q = 0; q < 26; q++) {
						forbiden[i][j][p][q] = false;
						cost[i][j][p][q] = -1;
					}
				}
			}
		}
		for (int i = 0; i < (int) f.size(); i++) {
			vector<string> tmp = split(f[i]);
			for (int a = 0; a < (int) tmp[0].size(); a++) {
				for (int b = 0; b < (int) tmp[1].size(); b++) {
					for (int c = 0; c < (int) tmp[2].size(); c++) {
						for (int d = 0; d < (int) tmp[3].size(); d++) {
							forbiden[tmp[0][a] - 'a'][tmp[1][b] - 'a'][tmp[2][c]
									- 'a'][tmp[3][d] - 'a'] = true;

						}
					}
				}
			}
		}
	}

	vector<string> split(string s) {
		vector<string> ret;
		stringstream ss(s);
		while (ss >> s) {
			ret.push_back(s);
		}
		return ret;
	}
};

